package com.example.server.controller;


import com.example.common.pojo.Menu;
import com.example.common.pojo.Role;
import com.example.server.service.IMenuService;
import com.example.server.service.IRoleService;
import com.example.common.vo.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 小红
 * @since 2022-09-02
 */
@RestController
@RequestMapping("/system/basic/permiss")
public class RoleController {

    @Autowired
    private IRoleService iRoleService;

    @Autowired
    private IMenuService iMenuService;


    @GetMapping("/")
    public List<Role> listRoles(){
        List<Role> list = iRoleService.list();
        return list;
    }

    @PostMapping("/role")
    public RespBean addRole(@RequestBody Role role){

        boolean save = iRoleService.save(role);
        return RespBean.success();
    }

    @DeleteMapping("/role/{id}")
    public RespBean deleteById(@PathVariable Integer id){
        boolean b = iRoleService.removeById(id);
        return RespBean.success();
    }

    @GetMapping("/menus")
    public List<Menu> listMenuLevel(){
        return iMenuService.findMenusLevel();
    }

    @GetMapping("/mid/{rid}")
    public List<Integer> listMenuIdsByRid(@PathVariable Integer rid){
        return iMenuService.findMenuIdsByRid(rid);
    }

    @PutMapping("/")
    public RespBean updateMenusByRole(Integer rid, Integer[] mids){
        return iMenuService.updateMenuRole(rid,mids);
    }
}
